﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using TravelLink.DAL;
using TravelLink.Common;

namespace TravelLink.WebApp.WS {
    /// <summary>
    /// TravelLinkDailyWebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class TravelLinkDailyWebService : System.Web.Services.WebService {

        [WebMethod]
        public string HelloWorld() {
            return "Hello World";
        }

        /// <summary>
        /// 获取最新职位
        /// </summary>
        /// <param name="count">请求条目</param>
        /// <returns></returns>
        [WebMethod]
        public LastedPostionResponse ListLastedPostion(int count) {
            var response = new LastedPostionResponse();
            try {
                if (count <= 0) throw new Exception("无效的请求条目！");
                else if (count > 100) throw new Exception("超出最大请求条目100！");
                using (TravelLinkDBDataContext db = DataContextFactory.Create()) {
                    var query = from p in db.TL_Company_Position
                                from c in db.TL_Company
                                where p.CompanyCode == c.CompanyCode
                                from a in db.TL_City
                                where p.WorkInCityId == a.Idx
                                orderby p.PublishDate descending
                                select new PositionInfo() {
                                    CommpanyName = c.CompanyName,
                                    PositionName = p.PositionName,
                                    PositionDescription = p.Description,
                                    Area = a.CName,
                                    PositionId = p.PositionCode,
                                    // PositionUrl = TravelLink.WebModel.Consts.PositionDetail(p.PositionCode)
                                };
                    response.Positions = query.Take(count).ToList().Select(u => new PositionInfo {

                        CommpanyName = u.CommpanyName,
                        PositionName = u.PositionName,
                        PositionDescription = u.PositionDescription,
                        Area = u.Area,
                        PositionId = u.PositionId,
                        PositionUrl = string.Concat(Utility.SiteRoot, TravelLink.WebModel.Consts.PositionDetail(u.PositionId))
                    }).ToList();
                }
            }
            catch (Exception ex) {
                response.ErrMsg = ex.Message;
            }
            return response;
        }
    }

    [Serializable]
    public class BaseResponse {

        private bool _isError;
        public bool IsError {
            get {
                _isError = string.IsNullOrEmpty(this.ErrMsg) ? false : true;
                return _isError;
            }
            set { _isError = value; }
        }

        public string ErrMsg {
            get;
            set;
        }
    }

    [Serializable]
    public sealed class LastedPostionResponse : BaseResponse {

        public List<PositionInfo> Positions { get; set; }
    }

    public class PositionInfo {

        public string CommpanyName { get; set; }

        public string PositionName { get; set; }

        public string PositionDescription { get; set; }

        public string Area { get; set; }

        public Guid PositionId { get; set; }

        public string PositionUrl { get; set; }
    }
}
